﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GRC_TPE_WebApp.Controller;

namespace GRC_TPE_WebApp.View.Private.Business
{
    public partial class AddBusiness : System.Web.UI.Page
    {
        int idClient = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            /// tentative de récupération de l'id
            if (!int.TryParse(Request.QueryString["id"], out idClient))
                /// redirection s'il y a une erreur au Parse
                Response.Redirect(Request.UrlReferrer.AbsolutePath);
        }

        /// <summary>
        /// Méthode déclenchée une fois la page chargée
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);

            if (this.idClient != 0)
            {
                string[] clientFields = ClientController.GetClientFields(this.idClient);

                ///si tous les champs ont bien été retournés
                if (clientFields.Count() == 4)
                    this.addBusinessClient.Text = clientFields[0];
                else
                    /// redirection s'il y a une erreur au Parse
                    Response.Redirect(Request.UrlReferrer.AbsolutePath);
            }
        }

        /// <summary>
        /// Permet d'ajouter une affaire après le clic sur le bouton "addBusinessButton" sur la page "AddBusiness.aspx"
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addBusinessButton_Click(object sender, EventArgs e)
        {
            decimal amount = 0;

            if(decimal.TryParse(this.addBusinessAmount.Text, out amount))
            {
                string ret = BusinessController.AddBusiness(this.addBusinessTitle.Text, this.DDL_State.SelectedValue, this.addBusinessDescription.Text, this.idClient.ToString(), amount);
                if (ret == "No error")
                {
                    this.addBusinessErrorMessage.ForeColor = System.Drawing.Color.DarkGreen;
                    this.addBusinessErrorMessage.Text = "Affaire ajoutée avec succès";
                    this.addBusinessTitle.Text = "";
                    this.addBusinessDescription.Text = "";
                    this.addBusinessAmount.Text = "";
                    this.DDL_State.SelectedIndex = 0;
                }
                else
                {
                    this.addBusinessErrorMessage.ForeColor = System.Drawing.Color.DarkRed;
                    this.addBusinessErrorMessage.Text = ret;
                }
            }
            else
            {
                this.addBusinessErrorMessage.ForeColor = System.Drawing.Color.DarkRed;
                this.addBusinessErrorMessage.Text = "Le montant doit être un nombre décimal.";
            }
        }
    }
}